package com.rbcs.base.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.struts2.json.annotations.JSON;
import org.hibernate.annotations.GenericGenerator;

import com.systop.common.modules.dept.model.Dept;
import com.systop.core.Constants;
import com.systop.core.model.BaseModel;

@SuppressWarnings("serial")
@Entity
@Table(name = "items")
public class Item extends BaseModel {

	private Integer id;

	private String name;
	
	//科目编码
	private String code;

	private String descn;
	
	//是否末端,默认为末端科目
	private String isLeaf = Constants.YES;
	
	//是否分摊科目
	private String isShare = Constants.NO;

	/** 上级科目 */
	private Item parent;

	/** Jquery.TreeTable 使用 */
	private String className = "item";


	/** 下级科目 */
	private Set<Item> subItems = new HashSet<Item>(0);

	/**
	 * 科目对应项目类别
	 */
	private ProjectType projectType;

	private Set<Dept> depts = new HashSet<Dept>(0);
	
	public Item(){
	}

	public Item(Integer id){
		this.id  = id;
	}
	
	@Transient
	public boolean getHasSubItems() {
		return subItems.size() > 0;
	}

	@Id
	@GeneratedValue(generator = "hibseq")
	@GenericGenerator(name = "hibseq", strategy = "hilo")
	@Column(name = "id", nullable = false)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDescn() {
		return descn;
	}

	public void setDescn(String descn) {
		this.descn = descn;
	}

	@JSON(serialize = false)
	@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
	@JoinColumn(name = "parent_id")
	public Item getParent() {
		return parent;
	}

	public void setParent(Item parent) {
		this.parent = parent;
	}

	@JSON(serialize = false)
	@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "parent")
	public Set<Item> getSubItems() {
		return subItems;
	}

	public void setSubItems(Set<Item> subItems) {
		this.subItems = subItems;
	}

	@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
	@JoinColumn(name = "project_type_id")
	public ProjectType getProjectType() {
		return projectType;
	}

	public void setProjectType(ProjectType projectType) {
		this.projectType = projectType;
	}

	@JSON(serialize = false)
	@ManyToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "items", targetEntity = Dept.class)
	public Set<Dept> getDepts() {
		return depts;
	}

	public void setDepts(Set<Dept> depts) {
		this.depts = depts;
	}
	
	@Column(name="is_share", length = 2)
	public String getIsShare() {
		return isShare;
	}

	public void setIsShare(String isShare) {
		this.isShare = isShare;
	}

	@Column(name = "is_leaf", length = 2)
	public String getIsLeaf() {
		return isLeaf;
	}

	public void setIsLeaf(String isLeaf) {
		this.isLeaf = isLeaf;
	}

	@Transient
	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

}
